package com.zhengdongzulin.zuke.app.main.mine.doorlock

import androidx.lifecycle.Observer
import com.google.gson.Gson
import com.lxj.androidktx.core.*
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ChangeHomeData
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityChangeLockPwdBinding
import com.zhengdongzulin.zuke.ext.contrast
import com.zhengdongzulin.zuke.ext.getTextZ


/**
 * jeff
 * 智能门锁修改密码
 * 2021-03-27 17:50:47
 */
class ZKChangeLockPwdActivity : BaseActivity<ChangeLockPwdVm, ZkActivityChangeLockPwdBinding>() {

    override fun getLayoutResId(): Int = R.layout.zk_activity_change_lock_pwd

    override fun initVM(): ChangeLockPwdVm = getVM(ChangeLockPwdVm::class.java)

    override fun initView() {
        setTitleContent("修改密码")
    }

    override fun initData() {

    }

    override fun initListener() {
        mView.getCode.setOnClickListener {
            mViewModel.send_code()
        }
        mView.commit.setOnClickListener {
            if (!mView.pwd.contrast(6)) {
                toast("密码不能少于6位")
                return@setOnClickListener
            }
            if (!mView.pwd.contrast(4)) {
                toast("验证码不能少于4位")
                return@setOnClickListener
            }

            mViewModel.update_lock_key_password(
                Gson().fromJson(
                    sp().getString("change_home", ""),
                    ChangeHomeData::class.java
                ).houseId,
                code = mView.code.getTextZ(),
                newPassWord = mView.pwd.getTextZ()
            )
        }
    }

    override fun startObserve() {
        mViewModel.update_lock_key_password.observe(this, Observer {
            toast("修改成功")
            finish()
        })
        mViewModel.update_lock_key_password.state.observe(
            this,
            getObserver(mViewModel.update_lock_key_password)
        )
        mViewModel.send_code.observe(this, Observer {
            toast("验证码发送成功")
        })
        mViewModel.send_code.state.observe(this, Observer {
            when (it) {
                StateLiveData.State.Error, StateLiveData.State.Empty -> {
                    toast(mViewModel.send_code.errMsg)
                    mView.getCode.cancelCountDown()
                }
            }
        })
    }

}